package cn.krund.lyc.hotlerobot.activity.ui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.TextView;

import cn.krund.lyc.hotlerobot.R;
import cn.krund.lyc.hotlerobot.activity.base.BaseActivity;
import cn.krund.lyc.hotlerobot.utils.Constant;
import cn.krund.lyc.hotlerobot.utils.MD5Util;

/**
 * @包名: cn.krund.lyc.hotlerobot.activity.ui
 * @作者: 刘育成
 * @时间: 2018/3/30  10:03
 * @org: 克路德人工智能
 * @描述: TODO
 * @fileName: PasswordActivity
 */

public class PasswordActivity extends BaseActivity {

    private TextView tv_show, tv_pw1, tv_pw2, tv_pw3, tv_pw4;                        // 输入的密码
    private String pw1, pw2, pw3, pw4;                                               // 输入后的密码
    private static final String DEFAULTPASSWORD = "1111";
    private String sysFlag;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sys_password;
    }

    @Override
    protected void initView() {
//获取接收参数:是否为系统密码访问
        sysFlag = this.getIntent().getStringExtra("sysFlag");
        if (sysFlag == null) {
            sysFlag = "1";
        }
        tv_show = (TextView) findViewById(R.id.tv_show);
        tv_pw1 = (TextView) findViewById(R.id.tv_pw1);
        tv_pw2 = (TextView) findViewById(R.id.tv_pw2);
        tv_pw3 = (TextView) findViewById(R.id.tv_pw3);
        tv_pw4 = (TextView) findViewById(R.id.tv_pw4);
    }

    @Override
    protected void initNev() {

    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.pw_num1:
                setInputNum("1");
                break;
            case R.id.pw_num2:
                setInputNum("2");
                break;
            case R.id.pw_num3:
                setInputNum("3");
                break;
            case R.id.pw_num4:
                setInputNum("4");
                break;
            case R.id.pw_num5:
                setInputNum("5");
                break;
            case R.id.pw_num6:
                setInputNum("6");
                break;
            case R.id.pw_num7:
                setInputNum("7");
                break;
            case R.id.pw_num8:
                setInputNum("8");
                break;
            case R.id.pw_num9:
                setInputNum("9");
                break;
            case R.id.pw_num0:
                setInputNum("0");
                break;
            case R.id.pw_num_delete:
                deleteInputPassWord();
                break;
            case R.id.pw_num_clear:
                clearInputPassWord();
                break;
        }
    }

    // 取到已填写的密码
    private void getInputNum() {
        pw1 = tv_pw1.getText().toString();
        pw2 = tv_pw2.getText().toString();
        pw3 = tv_pw3.getText().toString();
        pw4 = tv_pw4.getText().toString();
    }

    // 设置按下的数字
    private void setInputNum(String num) {
        getInputNum();
        if (pw1.isEmpty()) {
            tv_pw1.setText(num);
        } else if (pw2.isEmpty()) {
            tv_pw2.setText(num);
        } else if (pw3.isEmpty()) {
            tv_pw3.setText(num);
        } else if (pw4.isEmpty()) {
            tv_pw4.setText(num);
            getInputNum();
            judgePassWord();
        }
    }

    // 判断是否是第一次进入
    private void judgePassWord() {
        String pw = getSavePassWord();
        Intent intent;
        intent = new Intent(this, SettingActivity.class);

        if (pw == null) { // 首次使用默认密码
            if (isInputTrue(MD5Util.MD5(DEFAULTPASSWORD))) {
                if (!Constant.isLock) {
                    startActivity(intent);
                }
                finish();
            } else {//在这里添加管理员密码判断
                tv_show.setText("密码错误,请重新输入");
            }
        } else {
            if (isInputTrue(pw)) {
                if (!Constant.isLock) {
                    startActivity(intent);
                }
                finish();
            } else {//在这里添加管理员密码判断
                tv_show.setText("密码错误,请重新输入");
            }
        }
    }

    // 获取保存过得密码
    private String getSavePassWord() {
        SharedPreferences sharedPreferences = null;
        String savedPsw = null;
        sharedPreferences = getSharedPreferences("settingPassWord", MODE_PRIVATE);
        savedPsw = sharedPreferences.getString("savedSettingPassWord", null);
        return savedPsw;
    }

    // 判断输入的密码是否正确
    private boolean isInputTrue(String password) {
        StringBuffer inputPw = new StringBuffer();
        if (pw1.isEmpty() || pw2.isEmpty() || pw3.isEmpty() || pw4.isEmpty()) {
            return false;
        }
        inputPw.append(pw1).append(pw2).append(pw3).append(pw4);
        if (MD5Util.MD5(inputPw.toString()).equals(password)) {
            return true;
        }
        return false;
    }

    // 删除输入的密码
    private void deleteInputPassWord() {
        pw1 = tv_pw1.getText().toString();
        pw2 = tv_pw2.getText().toString();
        pw3 = tv_pw3.getText().toString();
        pw4 = tv_pw4.getText().toString();
        if (!pw4.isEmpty()) {
            tv_pw4.setText(null);
        } else if (!pw3.isEmpty()) {
            tv_pw3.setText(null);
        } else if (!pw2.isEmpty()) {
            tv_pw2.setText(null);
        } else if (!pw1.isEmpty()) {
            tv_pw1.setText(null);
            tv_show.setText("请输入密码");
        }
    }

    // 清空输入的密码
    private void clearInputPassWord() {
        tv_pw1.setText(null);
        tv_pw2.setText(null);
        tv_pw3.setText(null);
        tv_pw4.setText(null);
        tv_show.setText("请输入密码");
    }
}
